package vip.xiaonuo.common.util;

import cn.hutool.json.JSONObject;
import okhttp3.*;

import java.io.File;
import java.io.IOException;

/**
 * TODO 描述
 *
 * @author mengxiaowei
 * @date 2024/9/24 10:28
 */


public class CommonHttp {

    /**
     * 发送http请求
     *
     * @param [json, url]
     * @return java.lang.String
     * @author mengxiaowei
     * @date 2024/9/24 13:38
     **/
    public static String http(String json,String url,String method) throws IOException {
        OkHttpClient client = new OkHttpClient().newBuilder()
                .build();
        MediaType mediaType = MediaType.parse("application/json");
        RequestBody body = RequestBody.create(mediaType, json);
        Request request = new Request.Builder()
                .url(url)
                .method(method, body)
                .addHeader("Content-Type", "application/json")
                .addHeader("Cookie", "JSESSIONID=32CC0B1F40974447BEFC615F8CEDC03E")
                .build();
        Response response = client.newCall(request).execute();
        return response.body().toString();
    }


    public static String httpFile(String json,String url) throws IOException {
        OkHttpClient client = new OkHttpClient().newBuilder()
                .build();
        MediaType mediaType = MediaType.parse("text/plain");
        RequestBody body = new MultipartBody.Builder().setType(MultipartBody.FORM)
                .addFormDataPart("file",json,
                        RequestBody.create(MediaType.parse("application/octet-stream"),
                                new File(json)))
                .build();
        Request request = new Request.Builder()
                .url(url)
                .method("POST", body)
                .build();
        Response response = client.newCall(request).execute();
        return response.body().toString();
    }










}
